library(ltm)
library(parallel)
library(sjPlot)
library(doParallel)
library(textclean)
library(jtools)
library(cowplot)
library(fixest)
library(ggeffects)
library(gridExtra)
library(psych)
library(geomtextpath)
library(tm)
library(marginaleffects)
library(modelsummary)
library(kableExtra)
library(lmtest)
library(tidyverse)
library(data.table)
library(Ecume)
library(scales)

options("modelsummary_format_numeric_latex" = "plain")
options(modelsummary_factory_default = "kableExtra")
options(modelsummary_factory_latex = "kableExtra")


save <- c("db", "cleanup", "theme_bc", "save", "weighted.ttest.ci", "theme_bc",
          "census_race_ethnicity", "census_education", "census_income",
          "census_median_age", "clean_names")

cleanup <- function(...){
  save2 <- c(save, ...)
  rm(list=ls(envir = .GlobalEnv)[! ls(envir = .GlobalEnv) %in% save2], envir = .GlobalEnv)
  gc()
}

theme_bc <- function(base_size = 11, base_family = "",
                     legend.position = "right", face = "plain", ...) {
  library(extrafont)
  library(ggtext)
  half_line <- base_size/2
  theme_bw(base_family = base_family) %+replace%
    theme(plot.caption = element_textbox_simple(size = rel(0.8), hjust = 0,
                                                family = base_family,
                                                vjust = 1, margin = margin(t = half_line)),
          plot.title = element_text(size = rel(1.2), hjust = 0.5,
                                    vjust = 1, margin = margin(b = half_line),
                                    family = base_family),
          plot.subtitle = element_text(hjust = 0.5, vjust = 1, margin = margin(b = half_line)),
          legend.position = legend.position,
          text = element_text(family = base_family, face = face,
                              colour = "black", size = base_size, lineheight = 0.9,
                              hjust = 0.5, vjust = 0.5, angle = 0, margin = margin(),
                              debug = FALSE),
          plot.caption.position =  "plot",
          ...)
}

census_race_ethnicity <- function(geo, year, state = NULL, county = NULL){
  library(tidycensus)
  library(tidyverse)
  race_ethnicity <- get_acs(geography = geo,
                            variables = c(nh_white = "B03002_003",
                                          nh_black = "B03002_004",
                                          latino = "B03002_012",
                                          latino_black = "B03002_014",
                                          native_american = "B03002_005",
                                          hawaiian_pac_island = "B03002_007",
                                          asian = "B03002_006"),
                            summary_var = c(population = "B03002_001"),
                            state = state, county = county, year = year) %>%
    dplyr::mutate(estimate = estimate / summary_est) %>%
    dplyr::select(-ends_with("moe")) %>%
    dplyr::rename(population = summary_est) %>%
    spread(variable, estimate)
}

census_income <- function(geo, year, state = NULL, county = NULL){
  library(tidycensus)
  library(tidyverse)
  income <- get_acs(geography = geo,
                    variables = c(medincome = "B19013_001"),
                    state = state, county = county, year = year) %>%
    dplyr::select(-variable, -moe) %>%
    dplyr::rename(median_income = estimate)
}

census_education <- function(geo, year, state = NULL, county = NULL){
  library(tidycensus)
  library(tidyverse)
  education <- get_acs(geography = geo,
                       variables = c("B15002_012",
                                     "B15002_013",
                                     "B15002_014",
                                     "B15002_015",
                                     "B15002_016",
                                     "B15002_017",
                                     "B15002_018",
                                     
                                     "B15002_029",
                                     "B15002_030",
                                     "B15002_031",
                                     "B15002_032",
                                     "B15002_033",
                                     "B15002_034",
                                     "B15002_035"),
                       
                       summary_var = "B15002_001",
                       state = state, county = county, year = year) %>%
    dplyr::group_by(GEOID, NAME) %>%
    dplyr::summarize(some_college = sum(estimate / summary_est))
}

census_median_age <- function(geo, year, state = NULL, county = NULL){
  library(tidycensus)
  library(tidyverse)
  median_age <- get_acs(geography = geo,
                        variables = c(median_age = "B01002_001"),
                        state = state, county = county, year = year, output = "wide") %>%
    dplyr::select(GEOID, median_age = median_ageE)
}

clean_names <- function(data){
  return(gsub("[.]", "_", make.unique(make.names(tolower(colnames(data))))))
}